package com.yitiao.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.yitiao.config.CustomBlockExceptionHandler;
import com.yitiao.entity.CommerceUser;
import com.yitiao.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
@Slf4j
public class UserController {

    private final UserService userService;

    @GetMapping("/findById")
    public String getUserById(String id) {
        log.info("param is id : [{}]", id);
        return userService.getUserById(id).getUsername();
    }

    @GetMapping("/test")
    @SentinelResource(value = "/user/test",
            blockHandler = "userTestHandler",
            blockHandlerClass = CustomBlockExceptionHandler.class
    )
    public String test() {
        // 规范的日志打印，避免字符串拼接的开销
        if (log.isDebugEnabled()){
            log.debug("debug");
        }
        return "this is user test.";
    }

    @GetMapping("/add")
    public String addUser() {
        CommerceUser user = CommerceUser.builder().username("eventTest"+new Date()).build();
        userService.addUser(user);
        return "user add";
    }
}
